package org.ltsh.generate;

import com.alibaba.fastjson.JSONObject;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.ltsh.generate.config.GenerateConfig;
import org.ltsh.generate.config.GlobalConfig;
import org.ltsh.generate.config.ReadConfig;
import org.ltsh.generate.config.TempleteConfig;
import org.ltsh.generate.entity.GenerateEntity;
import org.ltsh.generate.entity.PackageGenerateEntity;

import org.ltsh.generate.templete.XmlTempleteUtils;

/**
 * Created by fengjianbo on 2017/12/26.
 */
@Mojo(name = "run")
public class GenerateMain extends AbstractMojo {

    @Parameter(defaultValue = "/templeteProject/autoConfig.xml")
    private String configPath;

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        System.out.println("ConfigPath:" + configPath);
        try {
            ReadConfig.readConfig(configPath);
            for (GenerateConfig generateConfig: GlobalConfig.generateConfigList) {
                for (TempleteConfig templeteConfig: GlobalConfig.templeteConfigList) {
                    if(generateConfig.getRefTemplete().equals(templeteConfig.getId())) {
                        GenerateEntity generateEntity = null;
                        if(generateConfig.getFileName().endsWith(".sql")) {
                            generateEntity = new GenerateEntity();
                        } else {
                            generateEntity = new PackageGenerateEntity();
                        }
                        generateEntity.setOutPath(templeteConfig.getOutPath());
                        generateEntity.setTempletePath(templeteConfig.getTempletePath());
                        generateEntity.setOutPath(templeteConfig.getOutPath());
                        generateEntity.setExtraParams(templeteConfig.getExtraMap());
                        generateEntity.setTableName(generateConfig.getTableName());
                        generateEntity.setFileName(generateConfig.getFileName());


                        generateEntity.setClassName(generateConfig.getClassName());
                        generateEntity.setLowerClassName(generateConfig.getLowerClassName());
                        if(templeteConfig.getSuffix() != null) {

                            generateEntity.setFileName(generateConfig.getClassName() + templeteConfig.getSuffix());
                        }
                        if(generateConfig.getFileName().endsWith(".sql")) {
                            XmlTempleteUtils.writeFile(generateEntity);
                        } else {
                            PackageGenerateEntity packageGenerateEntity = (PackageGenerateEntity) generateEntity;
                            packageGenerateEntity.setBasePackageStr(templeteConfig.getTargetPackage());
                            XmlTempleteUtils.writeFileForPage(packageGenerateEntity);
                        }
                        System.gc();
                    }
                }
            }

        } catch (Exception e) {
            System.out.println("error---------------------");
            e.printStackTrace();
        }
        System.out.println("执行完毕-------");
    }

}
